<?php
header("content-type:text/html; charset=utf-8");

if (!empty($_POST)) {

    require_once('../db/db.php');

    $type = $_POST['type'];//类型
    $title = addslashes(htmlspecialchars($_POST['title']));//标题
    $content = addslashes(htmlspecialchars($_POST['content']));//内容
    $time = $_POST['time'];//时间
    $time=$time==""?"0000-00-00":$_POST['time'];

    //插入语句
    $sql = "INSERT INTO news (type_new, title, content,ctime) VALUES ('$type', '$title', '$content','$time')";

    try {
        $connect = Db::getInstance()->connect();
    } catch (Exception $e) {
        echo '数据库链接失败:' . $e->getMessage();
        exit;
    }

    $result = mysqli_query($connect,$sql);
    if ($result) {
        echo "<script>alert('添加成功');window.location='index.php'</script>";
    } else {
        echo "<script>alert('添加失败');window.history.back();</script>";
    }

}

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>增加新闻</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/add.css" rel="stylesheet">
</head>

<body>
<div class="col-sm-9  col-md-10">
    <form class="form-horizontal" method="post" action="add.php">

        <div class="form-group">
            <label for="type" class="col-sm-2 control-label">类别</label>
            <div class="col-sm-10">
                <select class="form-control" name="type">
                    <option>新闻</option>
                    <option>军事</option>
                    <option>娱乐</option>
                    <option>笑话</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">标题</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="title" name="title" placeholder="请输入新闻标题"
                       required="required">
            </div>
        </div>

        <div class="form-group">
            <label for="content" class="col-sm-2 control-label">内容</label>
            <div class="col-sm-10">
                <textarea class="form-control" rows="3" id="content" name="content" placeholder="请输入新闻内容"
                          required="required" ></textarea>
            </div>
        </div>

        <div class="form-group">
            <label for="time" class="col-sm-2 control-label">时间</label>
            <div class="col-sm-10">
                <input class="form-control" type="date" id="time" name="time" placeholder="请输入新闻时间" required="required"/>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="reset" class="btn btn-primary">重置</button>
                <button type="submit" class="btn btn-success">提交</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>